package top.huntwolf.admin.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.huntwolf.admin.common.JsonResult;
import top.huntwolf.admin.pojo.book.Book;
import top.huntwolf.admin.pojo.book.BuyBook;
import top.huntwolf.admin.pojo.book.NewBook;
import top.huntwolf.admin.service.BookService;
import top.huntwolf.admin.service.CMSBookService;

import java.util.List;

@Api(description = "cms书本控制器")
@RestController
@CrossOrigin
@RequestMapping(value = "/cms/book")
public class CMSBookController {

    @Autowired
    private CMSBookService cmsBookService;

    @RequiresPermissions(value = "book:add")
    @PostMapping()
    @ApiOperation("添加书籍")
    public JsonResult addBuyBook(@RequestBody BuyBook book) {
        cmsBookService.addBuyBookByISBN(book);
        return JsonResult.success(null);
    }

    @RequiresPermissions(value = "book:find")
    @GetMapping("/stock")
    @ApiOperation("获取库存总数")
    public JsonResult<Integer> queryTotalBookInventory(){
        return JsonResult.success(cmsBookService.searchBookStock());
    }

    @RequiresPermissions(value = "book:find")
    @GetMapping("/info/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiOperation("获取书本信息")
    public JsonResult<PageInfo<BuyBook>> queryBookInfo(@PathVariable int pageNum, @PathVariable int pageSize){
        return JsonResult.success(cmsBookService.searchBookInfoByCMS(pageNum,pageSize));
    }

    @RequiresPermissions(value = "book:find")
    @GetMapping("/part")
    @ApiOperation("获取所有书本isbn和title信息")
    public JsonResult<List<Book>> queryBookIsbnAndTitle(){
        return JsonResult.success(cmsBookService.searchBookIsbnAndTitle());
    }


    @RequiresPermissions(value = "book:find")
    @GetMapping("/isbn/{isbn}/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiOperation("通过isbn查询")
    public JsonResult<PageInfo<BuyBook>> queryBookByIsbn(@PathVariable String isbn,@PathVariable int pageNum, @PathVariable int pageSize){
        return JsonResult.success(cmsBookService.searchBookInfoByIsbn(isbn,pageNum,pageSize));
    }

    @RequiresPermissions(value = "book:find")
    @GetMapping("/author/{author}/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiOperation("通过作者查询")
    public JsonResult<PageInfo<BuyBook>> queryBookByAuthor(@PathVariable String author,@PathVariable int pageNum, @PathVariable int pageSize){
        return JsonResult.success(cmsBookService.searchBookInfoByAuthor(author,pageNum,pageSize));
    }

    @RequiresPermissions(value = "book:find")
    @GetMapping("/title/{title}/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiOperation("通过书名查询")
    public JsonResult<PageInfo<BuyBook>> queryBookByTitle(@PathVariable String title,@PathVariable int pageNum, @PathVariable int pageSize){
        return JsonResult.success(cmsBookService.searchBookInfoByTitle(title,pageNum,pageSize));
    }

    @RequiresPermissions(value = "book:find")
    @GetMapping("/subjectId/{subjectId}/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiOperation("通过分类id查询")
    public JsonResult<PageInfo<BuyBook>> queryBookBySubjectId(@PathVariable String subjectId,@PathVariable int pageNum, @PathVariable int pageSize){
        return JsonResult.success(cmsBookService.searchBookInfoBySubjectId(subjectId,pageNum,pageSize));
    }


//    @GetMapping("/info/pageNum/{pageNum}/pageSize/{pageSize}")
//    @ApiOperation("获取书本信息(status无数据为未售,0为已购，1为已售)")
//    public JsonResult<PageInfo<NewBook>> queryBookInfo(@PathVariable int pageNum, @PathVariable int pageSize){
//        return JsonResult.success(bookStatusService.searchBookInfoByCMS(pageNum,pageSize));
//    }
//
//    @GetMapping("/isbn/{isbn}/pageNum/{pageNum}/pageSize/{pageSize}")
//    @ApiOperation("通过isbn查询(status无数据为未售,0为已购，1为已售)")
//    public JsonResult<PageInfo<NewBook>> queryBookByIsbn(@PathVariable String isbn,@PathVariable int pageNum, @PathVariable int pageSize){
//        return JsonResult.success(bookStatusService.searchBookInfoByIsbn(isbn,pageNum,pageSize));
//    }
//
//    @GetMapping("/title/{title}/pageNum/{pageNum}/pageSize/{pageSize}")
//    @ApiOperation("通过书名查询(status无数据为未售,0为已购，1为已售)")
//    public JsonResult<PageInfo<NewBook>> queryBookByTitle(@PathVariable String title,@PathVariable int pageNum, @PathVariable int pageSize){
//        return JsonResult.success(bookStatusService.searchBookInfoByTitle(title,pageNum,pageSize));
//    }

//    @PutMapping("/bookstatus")
//    @ApiOperation("修改书本状态")
//    public JsonResult updateBookStatus(@RequestBody List<bookStatus> bookStatuses){
//        bookStatusService.updateStatus(bookStatuses);
//        return JsonResult.success(null);
//    }

//    @DeleteMapping("/bookstatus/{id}")
//    @ApiOperation("删除书本状态")
//    public JsonResult deleteBookStatus(@PathVariable String id){
//        bookStatusService.deleteStatus(id);
//        return JsonResult.success(null);
//    }
//
//    @PostMapping("/bookstatus")
//    @ApiOperation("添加书本状态")
//    public JsonResult addBookStatus(@RequestBody bookStatus bookStatus){
//        bookStatusService.addStatus(bookStatus);
//        return JsonResult.success(null);
//    }

    @RequiresPermissions(value = "book:modify:subject")
    @PutMapping("/subject")
    @ApiOperation("修改书本分类")
    public JsonResult modifyBookSubject(@RequestBody List<BuyBook> books){
        cmsBookService.updateBookSubject(books);
        return JsonResult.success(null);
    }

    @RequiresPermissions(value = "book:find:api")
    @GetMapping("/fromApi/{isbn}")
    @ApiOperation("通过易源接口获取书籍")
    public JsonResult<BuyBook> queryBookByApi(@PathVariable String isbn){

        BuyBook book = cmsBookService.getBookDetailsFromApi(isbn);
        return JsonResult.success(book);

    }

}
